package com.wanger.swa.convert;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanger.swc.util.validation.MyVerifyUtil;
import com.wanger.swa.dto.templateDynamicField.req.TemplateDynamicFieldAddReq;
import com.wanger.swa.dto.templateDynamicField.req.TemplateDynamicFieldUpdateReq;
import com.wanger.swa.dto.templateDynamicField.resp.TemplateDynamicFieldResp;
import com.wanger.swa.domain.po.TemplateDynamicFieldPO;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: wanger
 * @Date: 2024/9/16 17:40
 * @Description: 模板动态字段
 */
@Component
public class TemplateDynamicFieldConvert {

    private TemplateDynamicFieldPO baseVerify(TemplateDynamicFieldPO po) {
        //进行通用数据验证
        MyVerifyUtil.nullVerify(po::getTemplateId, "模板id不能为空！");
        MyVerifyUtil.nullVerify(po::getName, "字段名称不能为空！");
        MyVerifyUtil.nullVerify(po::getPlaceholder, "占位符不能为空！");
        MyVerifyUtil.nullVerify(po::getType, "类型不能为空！");
        return po;
    }

    public TemplateDynamicFieldPO toSaveEntity(TemplateDynamicFieldAddReq addDTO) {
        return baseVerify(BeanUtil.copyProperties(addDTO, TemplateDynamicFieldPO.class));
    }

    public TemplateDynamicFieldPO toUpdateEntity(TemplateDynamicFieldUpdateReq updateDTO) {
        return baseVerify(BeanUtil.copyProperties(updateDTO, TemplateDynamicFieldPO.class));
    }

    public TemplateDynamicFieldResp toDetailsDTO(TemplateDynamicFieldPO templateDynamicField) {
        return toListDTO(CollUtil.toList(templateDynamicField))
                .stream()
                .findFirst()
                .orElse(null);
    }

    public List<TemplateDynamicFieldResp> toListDTO(List<TemplateDynamicFieldPO> list) {
        return BeanUtil.copyToList(list, TemplateDynamicFieldResp.class);
    }

    public IPage<TemplateDynamicFieldResp> toPageDTO(IPage<TemplateDynamicFieldPO> page) {
        return new Page<TemplateDynamicFieldResp>()
                .setRecords(toListDTO(page.getRecords()))
                .setTotal(page.getTotal())
                .setSize(page.getSize())
                .setCurrent(page.getCurrent())
                .setPages(page.getPages());
    }
}
